import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:laos_mall/common/style/app_style.dart';
import 'package:laos_mall/common/style/extension_util.dart';
import 'package:laos_mall/pages/main/book/view.dart';
import 'package:laos_mall/pages/main/catergory/view.dart';
import 'package:laos_mall/pages/main/home/view.dart';
import 'package:laos_mall/pages/main/my/view.dart';
import 'package:laos_mall/pages/main/shopping_cart/view.dart';
import 'package:laos_mall/user_data.dart';
import 'package:lazy_load_indexed_stack/lazy_load_indexed_stack.dart';

import 'logic.dart';

class MainPage extends StatelessWidget {
  MainPage({Key? key}) : super(key: key);
  final logic = Get.put(MainLogic());

  @override
  Widget build(BuildContext context) {
    return Obx(() =>
        Scaffold(
          body: LazyLoadIndexedStack(
            index: logic.index.value,
            children: [
              HomePage(),
              CatergoryPage(),
              BookPage(),
              ShoppingCartPage(),
              MyPage()
            ],
          ),
          // body: TabBarView(controller: logic.tabController,children: [
          //   HomePage(),
          //   CatergoryPage(),
          //   BookPage(),
          //   ShoppingCartPage(),
          //   MyPage()
          // ]),
          bottomNavigationBar: _buildBottomItem(),
        ));
  }

  _buildBottomItem() {

    return Obx(() {
     return
       BottomNavigationBar(
      // selectedIconTheme: IconThemeData(color: Colors.orange),
      // unselectedIconTheme: IconThemeData(color: Colors.white),
      type: BottomNavigationBarType.fixed,
      onTap: (index) {
        logic.index.value = index;
        // logic.tabController.animateTo(index);
        logic.tabController.index = index;
      },
      iconSize: 0,
      currentIndex: logic.index.value,
      items: logic.images
          .map((e) {
            int number = 0;
            switch(e){
              case "ic_tab_cart":
                number = UserData().cartNum.value;
                break;
            }
        return BottomNavigationBarItem(
        backgroundColor: AppColor.textColor,
        icon: Image.asset(
          e.toImgPath(),
          width: 20.w,
          height: 20.w,
        ).addBadge(number, offset: Offset(15.w, -12.w)),
        label: '',
        activeIcon: Image.asset(('${e}_sel').toImgPath(), width: 20.w,
          height: 20.w,).addBadge(number,offset: Offset(15.w, -12.w)));
      }
      )
          .toList(),
      );
    });
  }
}
